package com.chixing.controller;

import com.chixing.pojo.Customer;
import com.chixing.pojo.Hotel;
import com.chixing.pojo.Myorder;
import com.chixing.service.HotelService;
import com.chixing.service.Orderreservice;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
public class OrderreController {
@Autowired
private Orderreservice orderreservice;
    @Autowired
    private HotelService hotelService;

    @GetMapping("hotel/{id}")
       public ModelAndView getOrderHotel(@PathVariable("id") Integer id){
        ServerResponse serverResponse = orderreservice.getOrderHotel(id);
        ModelAndView mav = new ModelAndView();
        mav.addObject("serverResponse", serverResponse);
        mav.setViewName("order_payment/order_reserve");
        return mav;
    }
    //添加新订单
    @PostMapping("Myorder/myorder")
    @ResponseBody
    public ServerResponse save(Myorder myorder){
        return  orderreservice.save(myorder);
    }

    //添加新订单[新使用2021-11-3]
    @PostMapping("saveorder")
    /*public ModelAndView saveOrder(Myorder myorder, HttpSession session){
        System.out.println("myorder : " +myorder);*/
    public String saveOrder(Integer hotelId, Float orderPrice, String orderStartDate , String orderEndDate,HttpSession session){
        System.out.println("hotelId : " +hotelId);
        System.out.println("orderPrice : " +orderPrice);
        System.out.println("orderStartDate : " +orderStartDate);
        System.out.println("orderEndDate : " +orderEndDate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = sdf.parse(orderStartDate);
            endDate = sdf.parse(orderEndDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long days =  (endDate.getTime() - startDate.getTime()) / (60 * 60 * 24 * 1000);
       float totalPrice = orderPrice * days;
        System.out.println("需要住的天数：" + days);
        Customer customer =(Customer) session.getAttribute("loginCustomer");
        System.out.println("目前登录的用户id:" + customer.getCusId());
        System.out.println("住宿的总价格:" + totalPrice);


        Myorder myorder = new Myorder();
        String orderNum = UUID.randomUUID().toString().replace("-","");
        myorder.setOrderNumber(orderNum); // 订单编号
        myorder.setCusId(customer.getCusId()); // 用户id
        myorder.setHotelId(hotelId);
        myorder.setOrderPrice(totalPrice);
        myorder.setOrderStartDate(startDate);
        myorder.setOrderEndDate(endDate);
        myorder.setHotelInfoName(customer.getCusName());



        myorder.setOrderOther1(new Date().toString()); // 订单创建的时间
        ServerResponse serverResponse =  orderreservice.save(myorder);
        System.out.println("返回的结果是：" + serverResponse);

        return "redirect:myNewOrder?orderNum="+orderNum;

    }


    // 查询最新添加的订单
    @GetMapping("myNewOrder")
    public ModelAndView getMyNewOrder(@RequestParam("orderNum") String orderNum){
        ServerResponse serverResponse =  orderreservice.getMyNewOrder(orderNum);
        System.out.println("查询出我最新添加的订单是：" + serverResponse);

        Myorder order = (Myorder)serverResponse.getData();
        Hotel hotel =(Hotel) hotelService.getById(order.getHotelId()).getData();
        ModelAndView mav = new ModelAndView();
        mav.setViewName("order_payment/order_reserve");
        mav.addObject("order",order);
        mav.addObject("hotel",hotel);

        return mav;
    }
}
